#include "HttpServer.hpp"
#include <memory>

using namespace std;
using namespace server;

void Usage(std::string proc)
{
    cerr << "Usage: \n\t" << proc << "proc\r\n\r\n";
}

// 1. 服务器和网页分离，html
// 2. url -> / : web根目录
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    cout << "------------------- http start ------------------------------" << endl;
    cout << req.inbuffer << endl;
    std::cout << "method: " << req.method << std::endl;
    std::cout << "url: " << req.url << std::endl;
    std::cout << "httpversion: " << req.httpversion << std::endl;
    std::cout << "path: " << req.path << std::endl;
    cout << "------------------- http end ------------------------------" << endl;
    std::string respline = "HTTP/1.1 200 ok \r\n";          // 状态行
    std::string respheader = "Content-Type: text/html\r\n"; // 响应报头

    std::string respblank = "\r\n"; // 空行
    std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>Hello World</h1></head><body><p>每个进程都有自己的独立资源，因此需要注意资源的管理和释放。</p></body></html>";

    resp.outbuffer += respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;
    resp.outbuffer += body;
}

// ./httpserver 8080
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port));
    httpsvr->initServer();
    httpsvr->start();
}